package com.li.controller;

import com.li.common.config.Result;
import com.li.mapper.BadGoodsMapper;
import com.li.pojo.BadGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/badgoods")
public class BadGoodsController {
  @Autowired
  BadGoodsMapper badGoodsMapper;
  
  //获取公告列表
  @GetMapping
  public Result<?> getStudents(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer rows,
                               @RequestParam(defaultValue = "") String goods) {
    Map<String, Object> ret = new HashMap<String, Object>();
    Map<String, Object> queryMap = new HashMap<String, Object>();
    queryMap.put("offset", ((page - 1) * rows));
    queryMap.put("pageSize", rows);
    queryMap.put("goods", "%" + goods + "%");
    ret.put("rows", badGoodsMapper.findBadList(queryMap));
    ret.put("total", badGoodsMapper.getBadTotal(queryMap));
    return Result.success(ret);
  }
  
  /**
   * 新增
   *
   * @param
   * @return
   */
  @PostMapping
  public Result<?> save(@RequestBody BadGoods badGoods) {
    //自动更新创作时间
    badGoods.setSubtime(new Date());
    badGoodsMapper.addBadGoods(badGoods);
    return Result.success();
  }
  
  /**
   * 更新
   *
   * @param
   * @return
   */
  @PutMapping
  public Result<?> update(@RequestBody BadGoods badGoods) {
    badGoods.setRsotime(new Date());
    badGoodsMapper.updateById(badGoods);
    return Result.success();
  }
  
  @DeleteMapping("/{id}")
  public Result<?> deleteUser(@PathVariable Integer id) {
    Integer i = badGoodsMapper.deleteById(id);
    if (i == 0) {
      return Result.error("-1", "删除失败");
    }
    return Result.success();
  }
  
}
